﻿// -----------------------------------------------------------------------
// <copyright file="Horse.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Cspl.AnimalFight.Lib.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Cspl.AnimalFight.Lib.Interfaces;
    using Cspl.AnimalFight.Lib.EventHandlers;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Horse : AnimalBase, IDefend, IAttackable
    {
        #region Event
        public event DefendDelegate DefendEvent;
        #endregion

        #region Property
        public bool IsDefending
        { 
            get; 
            set; 
        }
        #endregion

        public void Defend()
        {
            if (IsExistOnBattleField("defend") && DefendEvent != null)
            {
                DefendEvent(this, new EventHandlers.DefendEventHandler
                    {
                        AnimalName = this.Name,
                        Target = Animals.Horse
                    });
            }
        }

        public override void Escape()
        {
            if (IsExistOnBattleField("escape"))
            {
                this.HasEscaped = true;
                Console.Write("The {0} has ", this.Name);
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine("escaped");
                Console.ResetColor();
            } 
        }

        public void Attacked(int ap)
        {

            if (IsExistOnBattleField("attack"))
            {


                int realDp = this.IsDefending ? this.DP + 10 : this.DP;

                this.HP -= (ap - realDp);
                this.HP = this.HP < 0 ? 0 : this.HP;


            Console.WriteLine("HP is decreased {0}", realDp - ap);
            //Console.WriteLine("Current HP for {0} = {1}", this.Name, this.HP);


            if (this.HP <= 0)
            {
                Console.Write("The {0} is ", this.Name);
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine("dead");
                Console.ResetColor();
                this.IsDead = true;
            }

                //set isDefending to false when it is true
                if (this.IsDefending)
                {
                    this.IsDefending = false;
                }    
            }
        }

        
    }
}
